from django.core.mail import send_mail
from django.conf import settings
from celery_tasks.main import app


@app.task(bind=True, name='send_verify_url')
def send_verify_url(self, email, verify_url):
    # 发送邮件
    try:
        res = send_mail(
            subject=settings.EMAIL_HOST,
            message=verify_url,
            from_email=settings.EMAIL_FROM,
            recipient_list=[email]
        )

    except Exception as e:
        res = -1

    # 2判断邮件发送是否发送成功

    if res == -1:
        print('邮件发送失败')
        '''
        ext : 没发送成功的报错信息
        count_down : 重发时间间隔
        max_retries : 重新发送的最大次数
        '''
        self.retry(exc=Exception('邮件发送失败'), countdown=5, max_retries=3)
